package com.smart.community.region.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smart.community.commons.enums.SystemStatusEnum;
import com.smart.community.region.entity.ZoneBuilding;
import com.smart.community.region.mapper.ZoneBuildingMapper;
import com.smart.community.region.service.IZoneBuildingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 分区楼栋关联服务实现类
 *
 * @author Wu.Liang
 * @since 2025-01-30
 */
@Slf4j
@Service
public class ZoneBuildingServiceImpl extends ServiceImpl<ZoneBuildingMapper, ZoneBuilding> implements IZoneBuildingService {

    @Override
    @Cacheable(value = "zone:building", key = "#zoneId", unless = "#result == null || #result.isEmpty()")
    public List<ZoneBuilding> getByZoneId(Long zoneId) {
        long startTime = System.currentTimeMillis();
        log.info("根据分区ID查询楼栋关联列表，分区ID: {}", zoneId);
        
        // 参数验证
        if (zoneId == null) {
            log.warn("分区ID不能为空");
            return Collections.emptyList();
        }
        
        try {
            LambdaQueryWrapper<ZoneBuilding> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ZoneBuilding::getZoneId, zoneId)
                       .eq(ZoneBuilding::getStatus, SystemStatusEnum.ENABLED.getCode());
            
            List<ZoneBuilding> result = this.list(queryWrapper);
            long endTime = System.currentTimeMillis();
            log.info("根据分区ID查询楼栋关联列表完成，分区ID: {}，结果数量: {}，耗时: {}ms", 
                    zoneId, result.size(), (endTime - startTime));
            
            return result != null ? result : Collections.emptyList();
        } catch (Exception e) {
            long endTime = System.currentTimeMillis();
            log.warn("根据分区ID查询楼栋关联列表失败，分区ID: {}，耗时: {}ms，错误: {}", 
                    zoneId, (endTime - startTime), e.getMessage());
            return Collections.emptyList();
        }
    }

    @Override
    @Cacheable(value = "zone:building", key = "'community:' + #communityId", unless = "#result == null || #result.isEmpty()")
    public List<ZoneBuilding> getByCommunityId(Long communityId) {
        long startTime = System.currentTimeMillis();
        log.info("根据社区ID查询楼栋关联列表，社区ID: {}", communityId);
        
        // 参数验证
        if (communityId == null) {
            log.warn("社区ID不能为空");
            return Collections.emptyList();
        }
        
        try {
            LambdaQueryWrapper<ZoneBuilding> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ZoneBuilding::getCommunityId, communityId)
                       .eq(ZoneBuilding::getStatus, SystemStatusEnum.ENABLED.getCode());
            
            List<ZoneBuilding> result = this.list(queryWrapper);
            long endTime = System.currentTimeMillis();
            log.info("根据社区ID查询楼栋关联列表完成，社区ID: {}，结果数量: {}，耗时: {}ms", 
                    communityId, result.size(), (endTime - startTime));
            
            return result != null ? result : Collections.emptyList();
        } catch (Exception e) {
            long endTime = System.currentTimeMillis();
            log.warn("根据社区ID查询楼栋关联列表失败，社区ID: {}，耗时: {}ms，错误: {}", 
                    communityId, (endTime - startTime), e.getMessage());
            return Collections.emptyList();
        }
    }

    @Override
    @Cacheable(value = "zone:building", key = "'building:' + #buildingId", unless = "#result == null")
    public ZoneBuilding getByBuildingId(Long buildingId) {
        long startTime = System.currentTimeMillis();
        log.info("根据楼栋ID查询分区关联，楼栋ID: {}", buildingId);
        
        // 参数验证
        if (buildingId == null) {
            log.warn("楼栋ID不能为空");
            return null;
        }
        
        try {
            LambdaQueryWrapper<ZoneBuilding> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ZoneBuilding::getBuildingId, buildingId)
                       .eq(ZoneBuilding::getStatus, SystemStatusEnum.ENABLED.getCode())
                       .last("LIMIT 1");
            
            ZoneBuilding result = this.getOne(queryWrapper);
            long endTime = System.currentTimeMillis();
            log.info("根据楼栋ID查询分区关联完成，楼栋ID: {}，结果: {}，耗时: {}ms", 
                    buildingId, result != null ? "找到" : "未找到", (endTime - startTime));
            
            return result;
        } catch (Exception e) {
            long endTime = System.currentTimeMillis();
            log.warn("根据楼栋ID查询分区关联失败，楼栋ID: {}，耗时: {}ms，错误: {}", 
                    buildingId, (endTime - startTime), e.getMessage());
            return null;
        }
    }

    @Override
    @CacheEvict(value = "zone:building", allEntries = true)
    public int removeByCommunityId(Long communityId) {
        long startTime = System.currentTimeMillis();
        log.info("根据社区ID删除楼栋关联，社区ID: {}", communityId);
        
        // 参数验证
        if (communityId == null) {
            log.warn("社区ID不能为空");
            return 0;
        }
        
        try {
            LambdaQueryWrapper<ZoneBuilding> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ZoneBuilding::getCommunityId, communityId);
            
            int result = this.baseMapper.delete(queryWrapper);
            long endTime = System.currentTimeMillis();
            log.info("根据社区ID删除楼栋关联完成，社区ID: {}，删除数量: {}，耗时: {}ms", 
                    communityId, result, (endTime - startTime));
            
            return result;
        } catch (Exception e) {
            long endTime = System.currentTimeMillis();
            log.warn("根据社区ID删除楼栋关联失败，社区ID: {}，耗时: {}ms，错误: {}", 
                    communityId, (endTime - startTime), e.getMessage());
            return 0;
        }
    }
}
